<?php
error_reporting ( 0 );
require_once ("in/tren.php");
//Format Menu
function add_topic($menuname, $game, $cont, $cat) {
	global $max_post;
	global $min_post;
	echo '<br><div class="navigation"><b>' . $menuname . '</b></div><div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div></div>';
	if ($cont == 0) {
		echo '<div class="link"><b style="color:red">Đang Trống</b></div>';
	} else
		while ( $ar = @mysql_fetch_array ( $game ) ) {
			$file = $ar ['link'];
			//$size = filesize ( 'fgame/' . $file . '' ) / 1000;
			//$size = getimagesize ( $file );
			//list($width, $height) = $size;
			echo '<table cellspacing="0" class="block"><tbody><tr>';
			//icon game$size = getimagesize
			if ($ar ['icon'] != '') {
				echo '<td class="img"><img src="fgame/' . $ar ['icon'] . '" alt="" height="30" width="30"></td>';
			} else {
				echo '<td class="img"><img src="fgame/icon.png" alt="" height="30" width="30"></td>';
			}
			//echo '<td><a href="game/?id=' . $ar ['id'] . '">' . $ar ['tieude'] . '</a><br/>Cỡ: ' . $size . 'kb<br/>' . $ar ['down'] . ' lượt tải.</td></tr></tbody></table><div style="border-top: 1px dotted gray; margin: 1px"></div>';
			echo '<td><a href="game/?id=' . $ar ['id'] . '">' . $ar ['tieude'] . '</a>&nbsp;(Lượt tải:&nbsp;' . $ar ['down'] . ')</td></tr></tbody></table><div style="border-top: 1px dotted gray; margin: 1px"></div>';
		}
	
		//Nếu số lượng > max_post thì thêm link "Xem thêm"
	//echo "cont:".$cont."; max:".$max_post;
	if ($cont > $max_post) {
		echo '<div align="right"><div class="xemthem1"><span class="xemthem2">&raquo;<a href="?ix=' . $cat . '">Xem Thêm...</a></span></div></div>';
	}
}

//Menu "Danh mục"
function load_menu() {
	echo '<br><div class="navigation"><b>Danh mục</b></div><div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div><div class="main_menu"><span style="color:#ff0099;">&raquo;</span><a href="game">Đến Danh Sách Thể Loại</a></div><div class="main_menu"><span style="color:#ff0099;">&raquo;</span><a href="game/tim.php">Tìm Kiếm Game</a></div><div class="main_menu"><span style="color: #ff0099;">&bull;</span>Tổng ' . mysql_result ( mysql_query ( "select count(*) from tlgame" ), 0 ) . ' thể loại</div><div class="main_menu"><span style="color: #ff0099;">&bull;</span>Tổng ' . mysql_result ( mysql_query ( "select count(*) from game" ), 0 ) . ' trò chơi</div><div class="main_menu"><span style="color: #ff0099;">&bull;</span>Đánh giá tốt ' . mysql_result ( mysql_query ( "select count(*) from dggame" ), 0 ) . '</div>';
}

//Mục CHAT
function load_chat() {
	global $set, $taikhoan;
	echo '<br><div class="navigation"><b>Chat Room</b></div><div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div>';
	
	$ql = 'op';
	if ($set ['chat'] != $ql && $taikhoan ['quyen'] != 2) {
		echo '<div class="main_menu"><b style="color:red">Phòng Chat Tạm Thời Đóng Cửa</b></div>';
	} else {
		echo '<div class="main_menu"><form action="chat/chat.php?chat=viet" method="post"><textarea cols="20" rows="2" name="noidung"></textarea><input type="submit" value="Gửi">  | <a href="?rf=' . rand ( 1, 20 ) . '">Tải Lại</a> | <a href="chat/bb.php">BBCode</a></form></div>';
	}
	if (! $_GET ['page']) {
		$_GET ['page'] = '1';
	}
	$page = $_GET ['page'];
	$chat = "SELECT count(*) FROM chat";
	$total_chat = mysql_query ( $chat );
	$num_chat = 5;
	$num_chat = ( int ) $num_chat;
	$lip = $page * 5;
	$lip = $lip - 5;
	$chat = "SELECT * FROM chat ORDER BY id DESC LIMIT $lip,$num_chat";
	$soob = mysql_query ( $chat );
	$count = mysql_result ( $total_chat, 0 );
	if ($count < 1) {
		echo '<div class="main_menu"><font color="red">Phòng Chat Trống</font></div>';
	} else
		while ( $v = mysql_fetch_array ( $soob ) ) {
			$id_chat = trim ( $v ['id'] );
			$id_chat = ( int ) $id_chat;
			$ten = $v ['ten'];
			$noidung = bbcode ( trim ( $v ['noidung'] ) );
			$tg = $v ['thoigian'];
			$i = $v ['ip'];
			echo '<div style="border-left:1px solid #00a6ff; border-bottom:1px solid #00a6ff; border-right:1px solid #00a6ff"><div style=" background:white; padding:2px; margin:2px"><div align="left"><div class="main_menu"><span style="color:#ff0099;">&bull;</span><b style="color:#00EEEE">' . $ten . '</b> <span style="text-align:right">[<a href="chat/chat.php?chat=q&id=' . $id_chat . '">Trích</a>]</span></div><font color="#555555">' . $noidung . '<br>(' . $tg . ')</font>';
			if ($taikhoan ['quyen'] == 2) {
				echo '<br>IP: ' . $i . '<br>____________<br>[<a href="chat/chat.php?chat=sua&id=' . $id_chat . '"><font color="blue">Sửa</font></a> | <a href="chat/chat.php?chat=xoa&id=' . $id_chat . '"><font color="blue">Xóa</font></a>]';
			}
			echo '</div></div></div>';
		}
}

switch ($_GET ['ix']) {
	default :
		$max_post = 5; //nếu số bài > max_post thì hiển thị chức năng "xem thêm"
		$min_post = 3; //số bài hiển thị tối đa của mỗi mục ở main page
		
		//Menu "Game mới nhất"
		$game = mysql_query ( "SELECT * FROM `game` ORDER BY `id` DESC LIMIT 3" );
		$cont = mysql_result ( mysql_query ( "select count(*) from `game`" ), 0 );
		add_topic ( "Game mới nhất", $game, $cont, "moinhat" );
		
		//Menu "Game tải nhiều nhất"
		$game = mysql_query ( "SELECT * FROM `game` WHERE `down` > 0 ORDER BY `down` DESC LIMIT 3;" );
		$cont = mysql_result ( mysql_query ( "select count(*) from `game` WHERE `down` > 0" ), 0 );
		add_topic ( "Game tải nhiều nhất", $game, $cont, "taive" );
		
		//Menu "Top Đánh giá"
		$game = mysql_query ( "SELECT * FROM `game` WHERE `thich` > 0 ORDER BY `thich` DESC LIMIT 3;" );
		$cont = mysql_result ( mysql_query ( "select count(*) from `game` WHERE `thich` > 0" ), 0 );
		add_topic ( "Game đánh giá hay nhất", $game, $cont, "danhgia" );
		
		//Menu "Danh mục"
		load_menu ();
		
		//Chat	
		//load_chat ();
		

		break;
	case 'moinhat' : //xem chi tiết mục mới nhất
		$tit = 'Top Lượt Xem';
		echo '<div class="navigation"><b>Game Mới Nhất</b></div>';
		echo '<div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div>';
		$game = mysql_query ( "SELECT * FROM `game` ORDER BY `id` DESC LIMIT 15;" );
		$cont = mysql_result ( mysql_query ( "select count(*) from `game`" ), 0 );
		if ($cont == 0) {
			echo '<div class="link"><b style="color:red">Đang Trống</b></div>';
		} else
			while ( $ar = @mysql_fetch_array ( $game ) ) {
				$file = $ar ['link'];
				$size = filesize ( 'fgame/' . $file . '' ) / 1000;
				echo '<table cellspacing="0" class="block"><tbody><tr>';
				echo '<td class="img"><img src="../fgame/' . $ar ['icon'] . '" alt="" height="30" width="30"></td>';
				echo '<td><span style="color: #ff0099;">&bull;</span><a href="game/?id=' . $ar ['id'] . '">' . $ar ['tieude'] . '</a><br/><span style="color: #ff0099;">&bull;</span>Cỡ: ' . $size . 'kb<br/><span style="color: #ff0099;">&bull;</span>' . $ar ['down'] . ' lượt tải.</td>';
				echo '</tr></tbody></table><div style="border-top: 1px dotted gray; margin: 1px"></div>';
			}
		echo '<div align="right"><div class="xemthem1"><span class="xemthem2">&laquo;<a href="?">Quay lại</a></span></div></div>';
		break;
	case 'danhgia' : //xem chi tiết mục "Đánh giá hay nhất"
		$tit = 'Top Đánh Giá';
		echo '<div class="navigation"><b>Top Đánh Giá</b></div><div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div></div>';
		$game = mysql_query ( "SELECT * FROM `game` WHERE `thich` > 0 ORDER BY `thich` DESC LIMIT 15;" );
		$cont = mysql_result ( mysql_query ( "select count(*) from `game` WHERE `thich` > 0" ), 0 );
		if ($cont == 0) {
			echo '<div class="link"><b style="color:red">Đang Trống</b></div>';
		} else
			while ( $ar = @mysql_fetch_array ( $game ) ) {
				$file = $ar ['link'];
				$size = filesize ( 'fgame/' . $file . '' ) / 1000;
				echo '<table cellspacing="0" class="block"><tbody><tr><td class="img"><img src="../fgame/' . $ar ['icon'] . '" alt="" height="30" width="30"></td><td><span style="color: #ff0099;">&bull;</span><a href="game/?id=' . $ar ['id'] . '">' . $ar ['tieude'] . '</a><br/><span style="color: #ff0099;">&bull;</span>Cỡ: ' . $size . 'kb<br/><span style="color: #ff0099;">&bull;</span>' . $ar ['down'] . ' lượt tải.</td></tr></tbody></table><div style="border-top: 1px dotted gray; margin: 1px"></div>';
			}
		echo '<div align="right"><div class="xemthem1"><span class="xemthem2">&laquo;<a href="?">Quay lại</a></span></div></div>';
		break;
	case 'taive' : //xem chi tiết mục "Tải về nhiều nhất"
		$tit = 'Top Tải Về';
		echo '<div class="navigation"><b>Top Tải Về</b></div><div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div></div>';
		$game = mysql_query ( "SELECT * FROM `game` WHERE `down` > 0 ORDER BY `down` DESC LIMIT 15;" );
		$cont = mysql_result ( mysql_query ( "select count(*) from `game` WHERE `down` > 0" ), 0 );
		if ($cont == 0) {
			echo '<div class="link"><b style="color:red">Đang Trống</b></div>';
		} else
			while ( $ar = @mysql_fetch_array ( $game ) ) {
				$file = $ar ['link'];
				$size = filesize ( 'fgame/' . $file . '' ) / 1000;
				echo '<table cellspacing="0" class="block"><tbody><tr><td class="img"><img src="../fgame/' . $ar ['icon'] . '" alt="" height="30" width="30"></td><td><span style="color: #ff0099;">&bull;</span><a href="game/?id=' . $ar ['id'] . '">' . $ar ['tieude'] . '</a><br/><span style="color: #ff0099;">&bull;</span>Cỡ: ' . $size . 'kb<br/><span style="color: #ff0099;">&bull;</span>' . $ar ['down'] . ' lượt tải.</td></tr></tbody></table><div style="border-top: 1px dotted gray; margin: 1px"></div>';
			}
		echo '<div align="right"><div class="xemthem1"><span class="xemthem2">&laquo;<a href="?">Quay lại</a></span></div></div>';
		break;
}
require_once ("in/duoi.php");
?>
